package com.ebt.m.fragment;

import android.content.Intent;

import com.ebt.m.AppInitService;
import com.ebt.m.activity.MainActivity;
import com.ebt.m.data.bean.UserInfo;
import com.ebt.m.commons.model.http.APIsFactory;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.commons.utils.Logger;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wanglu on 2017/8/31.
 */

public class AuthenticationFragment extends WebBaseFragment {
    @Override
    public boolean onBackPressed() {
        EBTAPI erpApi = AppInitService.getEbtApi();
        erpApi.isAuthenticate()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resultBaseDataResult -> {
                    UserInfo userInfo = resultBaseDataResult.getData();
                    if (userInfo.isRegisterCompany()) {
                        Intent intent = new Intent(getActivity(), MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                    } else {
                        if (!super.onBackPressed()) {
                            getActivity().finish();
                        }
                    }
                }, throwable -> {
                    try {
                        if (!super.onBackPressed()) {
                            getActivity().finish();
                        }
                    } catch (Exception e) {
                        Logger.e(e);
                    }
                    Logger.e(throwable.getMessage());
                });
        return true;
    }
}
